package com.note.app.controller;

import com.note.app.annotation.LogRecord;
import com.note.app.dto.SearchRequestDTO;
import com.note.app.dto.SearchResultDTO;
import com.note.app.service.SearchService;
import com.note.common.context.UserContext;
import com.note.common.vo.PageResult;
import com.note.common.vo.ResponseVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

import com.note.app.document.NoteSearchDocument;
import com.note.app.service.impl.SearchServiceImpl;

/**
 * 搜索控制器
 */
@Slf4j
@RestController
@RequestMapping("/search")
@Tag(name = "搜索管理", description = "笔记搜索相关接口")
public class SearchController {

    @Autowired
    @Qualifier("searchServiceImpl")
    private SearchServiceImpl elasticSearchService;
    
    @Autowired
    @Qualifier("mysqlSearchService")
    private SearchService mysqlSearchService;

    /**
     * 使用Elasticsearch搜索笔记
     *
     * @param searchRequest 搜索请求
     * @return 搜索结果
     */
    @PostMapping("/notes")
    @Operation(summary = "搜索笔记(ES)", description = "使用Elasticsearch搜索笔记")
    @LogRecord(description = "搜索笔记(ES)", module = "搜索管理", operationType = LogRecord.OperationType.QUERY)
    public ResponseVo<PageResult<SearchResultDTO>> searchNotes(@RequestBody SearchRequestDTO searchRequest) {
        // 获取当前用户ID
        Long userId = UserContext.getUserId();
        log.info("ES搜索请求 - 用户ID: {}, 请求参数: {}", userId, searchRequest);
        
        // 执行搜索
        PageResult<SearchResultDTO> result = elasticSearchService.searchNotes(userId, searchRequest);
        log.info("ES搜索完成 - 结果数量: {}, 总数: {}", 
                result.getRecords().size(), result.getTotal());
        
        return ResponseVo.success(result);
    }
    
    /**
     * 使用MySQL+MongoDB搜索笔记
     *
     * @param searchRequest 搜索请求
     * @return 搜索结果
     */
    @PostMapping("/mysql/notes")
    @Operation(summary = "搜索笔记(MySQL)", description = "使用MySQL全文索引搜索笔记")
    @LogRecord(description = "搜索笔记(MySQL)", module = "搜索管理", operationType = LogRecord.OperationType.QUERY)
    public ResponseVo<PageResult<SearchResultDTO>> searchNotesWithMysql(@RequestBody SearchRequestDTO searchRequest) {
        // 获取当前用户ID
        Long userId = UserContext.getUserId();
        log.info("MySQL搜索请求 - 用户ID: {}, 请求参数: {}", userId, searchRequest);
        
        // 执行搜索
        PageResult<SearchResultDTO> result = mysqlSearchService.searchNotes(userId, searchRequest);
        log.info("MySQL搜索完成 - 结果数量: {}, 总数: {}", 
                result.getRecords().size(), result.getTotal());
        
        return ResponseVo.success(result);
    }

    @GetMapping("/diagnostic")
    @Operation(summary = "ES诊断搜索", description = "执行简单ES查询用于诊断问题")
    public ResponseVo<List<String>> diagnosticSearch() {
        // 获取当前用户ID
        Long userId = UserContext.getUserId();
        log.info("执行ES诊断搜索 - 用户ID: {}", userId);
        
        // 调用诊断方法
        List<NoteSearchDocument> results = elasticSearchService.testSimpleSearch(userId);
        
        // 转换结果为简单字符串列表
        List<String> resultMessages = results.stream()
                .map(doc -> "笔记ID: " + doc.getNoteId() + ", 标题: " + doc.getTitle())
                .collect(Collectors.toList());
        
        // 如果没有结果，添加一条消息
        if (resultMessages.isEmpty()) {
            resultMessages.add("诊断完成，未找到任何匹配的笔记。");
        }
        
        return ResponseVo.success(resultMessages);
    }
}